// RestoreMdbDlg.cpp : implementation file
//

#include "stdafx.h"

#include "CDataFile.h"
#include "financedef.h"
#include "cshellfileop.h"

#include "RestoreMdb.h"
#include "RestoreMdbDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRestoreMdbDlg dialog

CRestoreMdbDlg::CRestoreMdbDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRestoreMdbDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRestoreMdbDlg)
	m_csRestoreFile = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	ZeroMemory( m_chBakPath, sizeof(m_chBakPath) );
}

void CRestoreMdbDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRestoreMdbDlg)
	DDX_Text(pDX, IDC_ED_RESTOREFILE, m_csRestoreFile);
	DDX_Control(pDX, IDC_BTN_RESTORE, m_btnRestore);
	DDX_Control(pDX, IDCANCEL, m_btnQuit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRestoreMdbDlg, CDialog)
	//{{AFX_MSG_MAP(CRestoreMdbDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_RESTORE, OnBtnRestore)
	ON_BN_CLICKED(IDC_BTN_RESDIR, OnBtnBrowsePath)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRestoreMdbDlg message handlers

BOOL CRestoreMdbDlg::OnInitDialog()
{
#ifdef _DEBUG
	CDataFile	IniDF( "..\\financecfg.ini" );
#else
	CDataFile	IniDF( MONEY_CFG );
#endif
	t_Str		sdStrTmp;
	DWORD		dwRet;

	
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// <> Init UI	
	m_btnRestore.SetIcon(IDI_RESTORE, IDI_RESTORE2);
	m_btnQuit.SetIcon(IDI_RUN2, IDI_RUN3);


	// <> Get current path segment and init edit
	sdStrTmp = IniDF.GetValue( "BakPath", "Backup segment" );
	if( 0 == sdStrTmp.length() )
	{
		dwRet = GetCurrentDirectory( MAX_PATH, m_chBakPath );
		if( dwRet == 0 ) 
		{
			sprintf( m_chBakPath, "GetCurrentDirectory failed (%d)\n", GetLastError() );
		}
		
		if( dwRet > MAX_PATH ) 
		{
			sprintf( m_chBakPath, "GetCurrentDirectory failed (buffer too small; need %d chars)\n", dwRet );
		}

		// otherwise is ok
	}
	else
	{
		sdStrTmp.copy( m_chBakPath, sdStrTmp.length(), 0 );
	}

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRestoreMdbDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRestoreMdbDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// 
// 
//--------------------------------------------------------------------------------
void CRestoreMdbDlg::OnBtnRestore() 
{
	CShellFileOp	op, opcpy;
	BOOL			bSuccess = FALSE, bAPICalled = FALSE, bAborted = FALSE;
	int				nAPIReturn = 0;
	CString			csAbsPath;
	CString			strMsg;
	CHAR			chMdb[100] = {0};


	// <> Dest abs path for backup file
	csAbsPath = m_csRestoreFile;
  	if( csAbsPath.IsEmpty() ) 
  	{
		return ;
	}

	// <>
#ifdef _DEBUG
	strcpy( chMdb, "E:\\Finance\\Mmoney.mdb" );
#else
	strcpy( chMdb, "Mmoney.mdb" );
#endif


	// <> Del old Mmoney.mdb
	op.SetOperationFlags(  FO_DELETE, this,
							FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_RENAMEONCOLLISION );

	op.AddSourceFile( chMdb );

	op.AddDestFile( chMdb );

	bSuccess = op.Go( &bAPICalled, &nAPIReturn, &bAborted );

	// <> Restore now
	if( bSuccess ) 
	{
		// <> Backup new bak.mdb
		opcpy.SetOperationFlags(  FO_COPY, this,
								FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_RENAMEONCOLLISION );
 
		opcpy.AddSourceFile( csAbsPath );

		opcpy.AddDestFile( chMdb );

		bSuccess = opcpy.Go( &bAPICalled, &nAPIReturn, &bAborted );
		if( bSuccess ) 
		{
			MessageBox( "Restore successfully!", "Remind", MB_ICONINFORMATION );
		}
		else
		{
			MessageBox( "Restore unsuccessfully!\nPlease restore the Mmoney.mdb manually!", "Remind", MB_ICONWARNING );
		}
	}
	else
	{
		strMsg.Format( _T("CShellFileOp::Go() returned %s.\n\nbAPICalled = %s\nnAPIReturn = %d\nbAborted = %s"),
						 bSuccess ? _T("TRUE") : _T("FALSE"),
						 bAPICalled ? _T("TRUE") : _T("FALSE"),
						 nAPIReturn,
						 bAborted ? _T("TRUE") : _T("FALSE") );
 
		 AfxMessageBox( strMsg, MB_ICONINFORMATION );
	}
}

// 
// 
//--------------------------------------------------------------------------------
void CRestoreMdbDlg::OnBtnBrowsePath() 
{
	CHAR			chFile[ MAX_PATH ] = {0};
	OPENFILENAME	OpName;



	ZeroMemory( &OpName, sizeof(OPENFILENAME) );

	OpName.lStructSize	= sizeof(OPENFILENAME);
	OpName.hwndOwner	= m_hWnd;
	OpName.lpstrFile	= chFile;
	OpName.nMaxFile		= sizeof(chFile);
	OpName.Flags			= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
	OpName.nFilterIndex		= 1;
	OpName.lpstrFileTitle	= NULL;
	OpName.nMaxFileTitle	= 0;
	OpName.lpstrInitialDir	= m_chBakPath;	// default is the bakup path in ini file
	OpName.lpstrFilter	= _T( "Open backup mdb(*.mdb)\0*.mdb\0" );
	
	if( !GetOpenFileName(&OpName) )
	{
		return ;
	}

	m_csRestoreFile = chFile;

	UpdateData(FALSE);
	
}
